﻿function ShowAjaxError(e, prex) {
	if (e != null) {
		if (prex instanceof String) {
			prex += '：';
		} else {
			prex = "";
		}
		var msg = "";
		if (e.responseText) {
			msg = prex + e.responseText;
		} else {
			msg = prex + e.statusText;
		}
		if (msg.length > 600) {
			$.messager.alert({
				title: '提示',
				maximizable: true, resizable: true,
				msg: '<div style="height:450px;">' + msg + '</div>',
				width: 800
			});
		} else {
			$.messager.alert('错误', msg, 'error');
		}
	}
}

//编辑
function GridRowEditFmt(callBack, id) {
	if (typeof (id) == "string") {
		return "<a href='javascript:" + callBack + "(\"" + id + "\")' class='grid_icon grid_edit' title='修改'><i class='fa fa-pencil-square-o'></i></a>";
	} else {
		return "<a href='javascript:" + callBack + "(" + id + ")' class='grid_icon grid_edit' title='修改'><i class='fa fa-pencil-square-o'></i></a>";
	}
}



//删除
function GridRowDeleteFmt(callBack, id) {
	if (typeof (id) == "string") {
		return "<a href='javascript:" + callBack + "(\"" + id + "\")' class='grid_icon grid_delete' title='删除'><i class='fa fa-remove'></i></a>";
	} else {
		return "<a href='javascript:" + callBack + "(" + id + ")' class='grid_icon grid_delete' title='删除'><i class='fa fa-remove'></i></a>";
	}
}

//审核
function GridRowAuditFmt(callBack, id) {
	if (typeof (id) == "string") {
		return "<a href='javascript:" + callBack + "(\"" + id + "\")' class='grid_icon grid_audit' title='审核'><i class='fa fa-exclamation-circle'></i></a>";
	} else {
		return "<a href='javascript:" + callBack + "(" + id + ")' class='grid_icon grid_audit' title='审核'><i class='fa fa-exclamation-circle'></i></a>";
	}
}

//功能
function GridRowOperationFmt(callBack, id, title, icon, bgColor) {
	if ($.IsNullOrEmpty(icon)) { icon = "fa fa-tasks"; }
	if (!$.IsNullOrEmpty(bgColor)) { bgColor = "background-color:"+bgColor; }
	if (typeof (id) == "string") {
		return "<a href='javascript:" + callBack + "(\"" + id + "\")' class='grid_operation' title='" + title + "' style='" + bgColor+"'><i class='" + icon+"'></i><span class='text'>" + title +"</span></a>";
	} else {
		return "<a href='javascript:" + callBack + "(" + id + ")' class='grid_operation' title='" + title + "' style='" + bgColor +"'><i class='" + icon +"'></i><span class='text'>" + title +"</span></a>";
	}
}

///加载对象
function LoadObject(id, url, callback) {
	if ((typeof (id) == "number" && id > 0) || (typeof (id) == "string" && $.IsNullOrEmpty(id) == false)) {
		if (!url.endsWith("/")) { url = url + "/" + id; }
		else { url = url + id; }

		$.ajax({
			method: "GET",
			url: url,
			success: callback,
			error: function (e) {
				ShowAjaxError(e);
			}
		});
	}
}

//获取对象(独立参数)
function GetObject(id, url, force) {
	if ((typeof (id) == "number" && id > 0) || (typeof (id) == "string" && $.IsNullOrEmpty(id) == false)) {
		if (!url.endsWith("/")) { url = url + "/" + id; }
		else { url = url + id; }

		return $.JC(url).Get3(url, null, function (k) {
			var model = null;
			$.ajax({
				async: false,
				method: 'GET',
				url: url,
				success: function (data) {
					if (data !== null) {
						model = data;
					}
				}
			});
			return model;
		}, force);
	}

	return null;
}

//获取对象(URL中带参数)
function GetUrlObject(url, force) {
	return $.JC(url).Get3(url, null, function (k) {
		var model = null;
		$.ajax({
			async: false,
			method: 'GET',
			url: url,
			success: function (data) {
				if (data !== null) {
					model = data;
				}
			}
		});
		return model;
	}, force);
}


////////////////////////////
//绑定菜单
function MySideMenu(menus, p) {
	if (p == undefined) { p = 5; }
	var c = _MySideMenu(menus, p);
	c = "<ul class='my_side_menu'>" + c.substr(4);
	return c;
}

function _MySideMenu(menus, p) {
	var content = "<ul>";
	for (var i = 0; i < menus.length; i++) {
		var fm = "li_menu";
		var hasChild = menus[i].children != null && menus[i].children.length > 0;
		if (hasChild && menus[i].CanFold) { fm += " current_folder"; }

		content += "<li id='" + menus[i].Id + "' canfold='" + menus[i].CanFold + "' src='" + menus[i].Url + "' code='" + menus[i].Code+"' class='" + fm + "' style='padding-left:" + p + "px' onclick='_OnMySideMenu(this)'>";
		content += "<i class='" + menus[i].IconClassName + " icon'></i>";
		content += "<span>" + menus[i].Name + "</span>";
		if (hasChild && menus[i].CanFold) { content += "<i class='fa fa-angle-down array'></i>"; }
		content += "</li>";
		if (hasChild) {
			if (menus[i].Expanded) {
				content += "<li>" + _MySideMenu(menus[i].children, p + 15) + "</li>";
			} else {
				content += "<li style='display:none'>" + _MySideMenu(menus[i].children, p + 15) + "</li>";
			}
		}
	}
	content += "</ul>";

	return content;
}

function _OnMySideMenu(li) {
	var lm = "li_menu";
	var fm = "current_folder";
	var cm = "current_li_menu";
	var m = $(li).parents(".my_side_menu");
	var p = m.find("." + cm);
	if ($(li).hasClass(lm)) {
		var n = $(li).next();
		if (n.length > 0) {
			if (n.hasClass(lm)) {
				p.removeClass(cm);
				$(li).addClass(cm);
			} else {
				if ($(li).attr("canfold") == "true") {
					var ay = $(li).find(".array");
					if (n.is(":hidden")) {
						$(li).addClass(fm);
						ay.removeClass("fa-angle-up").addClass("fa-angle-down");
					} else {
						$(li).removeClass(fm);
						ay.removeClass("fa-angle-down").addClass("fa-angle-up");
					}
					n.toggle(300);
				} else {
					p.removeClass(cm);
					$(li).addClass(cm);
				}
			}
		} else {
			p.removeClass(cm);
			$(li).addClass(cm);
		}
	}
}

///绑定默认事件
function BindMySideMenu(sm) {
	sm.find(".li_menu").each(function () {
		var n = $(this).next();
		if (n.length > 0) {
			$(this).click(function (e) {
				if (e.target.tagName !== 'LI') {
					$.App._LoadContentPanel($(e.target).parent());
				} else {
					$.App._LoadContentPanel(e.target);
				}
			});
		} else {
			$(this).click(function (e) {
				if (e.target.tagName !== 'LI') {
					$.App._LoadContentPanel($(e.target).parent());
				} else {
					$.App._LoadContentPanel(e.target);
				}
			});
		}
	});
}

function LoadFirstMySideMenu(p) {
	$(p).find(".li_menu").each(function(){
		var src = $(this).attr("src");
		if (!$.IsNullOrEmpty(src) && src != "null") {
			$(this).click();
		}
		return false;
	});
}